Skip to content

caching

Utilities for managing the cache.

Attributes⚓︎

Classes⚓︎

Functions⚓︎

init_cache ⚓︎

init_cache(path_cache_dir)

Ensure that the cache directory exists and add the README.

PARAMETER DESCRIPTION
path_cache_dir

location of the cache directory

TYPE: Path

Source code in pytest_cache_assert/_check_assert/caching.py
@beartype
def init_cache(path_cache_dir: Path) -> None:
    """Ensure that the cache directory exists and add the README.

    Args:
        path_cache_dir: location of the cache directory

    """
    existed = path_cache_dir.is_dir()
    path_cache_dir.mkdir(exist_ok=True, parents=True)
    pth_readme = (path_cache_dir / 'README.md')
    if not (existed or pth_readme.is_file()):
        pth_readme.write_text(CACHE_README_TEXT)

load_cached_data ⚓︎

load_cached_data(path_cache_file)

Cache the specified data.

PARAMETER DESCRIPTION
path_cache_file

location of the cache file to write

TYPE: Path

RETURNS DESCRIPTION
Any

loaded data from cache file

TYPE: Any

Source code in pytest_cache_assert/_check_assert/caching.py
@beartype
def load_cached_data(path_cache_file: Path) -> Any:
    """Cache the specified data.

    Args:
        path_cache_file: location of the cache file to write

    Returns:
        Any: loaded data from cache file

    """
    if path_cache_file.is_file():
        return _read_full_cache(path_cache_file)[KEY_NAME_DATA]
    raise NoCacheError(path_cache_file)

write_cache_data ⚓︎

write_cache_data(path_cache_file, *, metadata, test_data, always_write=False)

Cache the specified data.

PARAMETER DESCRIPTION
path_cache_file

location of the cache file to write

TYPE: Path

metadata

optional dictionary for storing in the cache file

TYPE: Optional[Dict]

test_data

arbitrary test data to store

TYPE: Any

always_write

if True, overwrite the cached data

TYPE: bool DEFAULT: False

Source code in pytest_cache_assert/_check_assert/caching.py
@beartype
def write_cache_data(
    path_cache_file: Path,
    *,
    metadata: Optional[Dict],  # type: ignore[type-arg]
    test_data: Any,
    always_write: bool = False,
) -> None:
    """Cache the specified data.

    Args:
        path_cache_file: location of the cache file to write
        metadata: optional dictionary for storing in the cache file
        test_data: arbitrary test data to store
        always_write: if True, overwrite the cached data

    """
    metadata = make_diffable(metadata or {})
    meta = [metadata or {}]
    if path_cache_file.is_file():
        old_cache_dict = _read_full_cache(path_cache_file)
        old_meta = old_cache_dict[KEY_NAME_META]
        meta = _merge_metadata(meta[0], old_meta)
        if not always_write:  # Only change test_data if `always_write`
            test_data = old_cache_dict[KEY_NAME_DATA]

    cache_dict = {KEY_NAME_META: meta, KEY_NAME_DATA: test_data}
    path_cache_file.parent.mkdir(exist_ok=True, parents=True)
    path_cache_file.write_text(pretty_dumps(cache_dict))

Last update: August 30, 2023
Created: August 30, 2023